# nodes.py
# 定义 SQL 语法树的节点类型

class Node:
    """
    抽象语法树（AST）的基类
    """
    def __init__(self):
        self.children = []

    def add_child(self, child):
        self.children.append(child)

    def __repr__(self):
        return self.__class__.__name__


class CreateTableNode(Node):
    """
    CREATE TABLE 语句的节点
    例如：CREATE TABLE users (id, name, age);
    """
    def __init__(self, table_name, columns):
        super().__init__()
        self.table_name = table_name  # Token 对象，类型为 IDENTIFIER
        self.columns = columns        # 列名列表，如 ['id', 'name', 'age']

    def __repr__(self):
        return f"CreateTableNode(table_name={self.table_name}, columns={self.columns})"


class InsertNode(Node):
    """
    INSERT INTO 语句的节点
    例如：INSERT INTO users VALUES (1, 'Alice', 25);
    """
    def __init__(self, table_name, values):
        super().__init__()
        self.table_name = table_name  # Token 对象
        self.values = values          # 值列表，如 [1, 'Alice', 25]

    def __repr__(self):
        return f"InsertNode(table_name={self.table_name}, values={self.values})"


class SelectNode(Node):
    """
    SELECT 语句的节点
    例如：SELECT name, age FROM users WHERE age > 20;
    """
    def __init__(self, fields, table, where_condition=None):
        super().__init__()
        self.fields = fields              # 要查询的字段列表，如 [Token(name), Token(age)]
        self.table = table                # 表名 Token
        self.where_condition = where_condition  # 可选的 WHERE 条件，格式为 (left, op, right)

    def __repr__(self):
        return f"SelectNode(fields={self.fields}, table={self.table}, where_condition={self.where_condition})"